﻿using System;
using System.Runtime.CompilerServices;
using System.Text;
using System.Web.Mvc;
 
public static class HtmlHelperExtensions
{
    private static string ConvertToJS(string value)
    {
        return value.Replace("'", "'").Replace("\n", @"\n").Replace("\r", @"\r");
    }

    public static string FckEditor(this HtmlHelper html, string name)
    {
        return html.FckEditor(name, Convert.ToString(html.ViewData.Eval(name)), new FckConfig());
    }

    public static string FckEditor(this HtmlHelper html, string name, FckConfig objFckConfig)
    {
        return html.FckEditor(name, Convert.ToString(html.ViewData.Eval(name)), objFckConfig);
    }

    public static string FckEditor(this HtmlHelper html, string name, string value, FckConfig objFckConfig)
    {
        StringBuilder builder = new StringBuilder();
        builder.Append("<script type=\"text/javascript\">\n");
        builder.Append("<!--\n");
        builder.Append("var oFCKeditor = new FCKeditor('" + name + "');\n");
        builder.AppendFormat("oFCKeditor.BasePath='{0}';", objFckConfig.BasePath);
        if (objFckConfig.Height != "100%")
        {
            builder.Append("oFCKeditor.Width = '" + objFckConfig.Width + "';\n");
        }
        if (objFckConfig.Height != "300")
        {
            builder.Append("oFCKeditor.Height = '" + objFckConfig.Height + "';\n");
        }
        if (objFckConfig.ToolbarSet == FckToolbarSet.Basic)
        {
            builder.Append("oFCKeditor.ToolbarSet = 'Basic';\n");
        }
        if (objFckConfig.Skin == FckSkin.Office2003)
        {
            builder.Append("oFCKeditor.Config['SkinPath'] = oFCKeditor.BasePath + 'editor/skins/office2003/';\n");
        }
        if (objFckConfig.Skin == FckSkin.Silver)
        {
            builder.Append("oFCKeditor.Config['SkinPath'] = oFCKeditor.BasePath + 'editor/skins/silver/';\n");
        }
        builder.Append("oFCKeditor.Value = '" + ConvertToJS(value) + "';\n");
        builder.Append("oFCKeditor.Create() ;\n");
        builder.Append("//-->\n");
        builder.Append("</script>\n");
        return builder.ToString();
    }
}

